<?php
require_once("mysql.inc.php");
require_once("checkLogin.inc.php");

if(isset($_GET["username"]) && isset($_GET["group"])) {
	getUser($_GET["username"], $_GET["group"]);
}

function getUser($username, $group) {

	$username = mysql_real_escape_string($username);
	$group = mysql_real_escape_string($group);
	
	$result = mysql_query("SELECT * FROM `users` WHERE `username`='$username' AND `group`='$group'") or die(mysql_error());
	$fetch = mysql_fetch_array($result);
	
	$no_info = "Niet bekend";
	$j = 1;
	
	//create new dom document
	$doc = new DomDocument('1.0');
	$root = $doc->createElement("results");
	$doc->appendChild($root);
	
	if($fetch["role"] == "TopBeheerder") {
		$admin = "true";
		$topadmin = "true";
	}
	else if($fetch["role"] == "Beheerder") {
		$admin = "true";
		$topadmin = "false";
	}
	else {
		$admin = "false";
		$topadmin = "false";
	}
	
	$u = $doc->createElement("name");
	if(!empty($fetch["username"])) {
		$u->appendChild($doc->createTextNode($fetch["username"]));
	}
	else {
		$u->appendChild($doc->createTextNode($no_info));
	}		
	$root->appendChild($u);
	
	$g = $doc->createElement("group");
	if(!empty($fetch["group"])) {
		$g->appendChild($doc->createTextNode($fetch["group"]));
	}
	else {
		$g->appendChild($doc->createTextNode($no_info));
	}		
	$root->appendChild($g);
	
	$a = $doc->createElement("admin");
	$a->appendChild($doc->createTextNode($admin));
	$root->appendChild($a);
	
	$t = $doc->createElement("topAdmin");
	$t->appendChild($doc->createTextNode($topadmin));
	$root->appendChild($t);
	
	for($i=1; $i<=5; $i++) {
		
		if(!empty($fetch["id$i"])) {
		$id = $fetch["id$i"];
		
		$result2 = mysql_query("SELECT * FROM `books` WHERE `id`='$id'") or die(mysql_error());
		$fetch2 = mysql_fetch_array($result2);
		
		$title = $doc->createElement("title$j");
		$title->appendChild($doc->createTextNode($fetch2["title"]));
		$root->appendChild($title);
		
		$author = $doc->createElement("author$j");
		$author->appendChild($doc->createTextNode($fetch2["author"]));
		$root->appendChild($author);
		
		$d = $doc->createElement("id$j");
		$d->appendChild($doc->createTextNode($id));
		$root->appendChild($d);
		
		$date = $doc->createElement("date$j");
		if(!empty($fetch["date$i"])) {
			$date->appendChild($doc->createTextNode($fetch["date$i"]));
		}
		else {
			$date->appendChild($doc->createTextNode($no_info));
		}
		$root->appendChild($date);
		
		$j++;
		}
	}
	
	$j = $j - 1;
	
	$num = $doc->createElement("num");
	$num->appendChild($doc->createTextNode($j));
	$root->appendChild($num);
	
	echo $doc->saveXML();
}